﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.WorkCenter_;
using THE_WIND_RISES.Common.Dtos;
namespace Production_Set.API.Application.Commands.WorkCenters
{
    /// <summary>
    /// 工作中心的修改
    /// </summary>
    public class WorkCenterUpdHandler : IRequestHandler<WorkCenterUpdCommand, Result>
    {
        private readonly IBaseRepository<WorkCenter> WorkCenterRepository;
        private readonly ILogger<WorkCenterAddHandler> logger;

        public WorkCenterUpdHandler(IBaseRepository<WorkCenter> workCenterRepository, ILogger<WorkCenterAddHandler> logger)
        {
            WorkCenterRepository = workCenterRepository;
            this.logger = logger;
        }

        public async Task<Result> Handle(WorkCenterUpdCommand request, CancellationToken cancellationToken)
        {
            var result = new THE_WIND_RISES.Common.Dtos.Result() { Code = (int)CommonErrorCodes.Update };
            try
            {
                var WorkCenterSql = $"Update workCenters SET WorkCenterId= {request.WorkCenterId},WorkCenterNum = '{request.WorkCenterNum}',WorkCenterName = '{request.WorkCenterName}',WorkCenterType = '{request.WorkCenterType}',ClassesData = '{request.ClassesData}',CreateTime = '{request.CreateTime}',CreateName = '{request.CreateName}',WorkCenterState = {request.WorkCenterState},ApprovalState = {request.ApprovalState},IsDelWorkCenter = {false} WHERE WorkCenterId = {request.WorkCenterId};";
                var UpdRes = WorkCenterRepository.Update(WorkCenterSql, new
                {
                    WorkCenterId = request.WorkCenterId,
                    WorkCenterNum = request.WorkCenterNum,
                    WorkCenterName = request.WorkCenterName,
                    WorkCenterType = request.WorkCenterType,
                    ClassesData = request.ClassesData,
                    CreateTime = request.CreateTime,
                    CreateName = request.CreateName,
                    WorkCenterState = request.WorkCenterState,
                    ApprovalState = request.ApprovalState,
                    IsDelWorkCenter = request.IsDelWorkCenter
                });
                if (UpdRes > 0)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = null;
                    result.Msg = "工作中心的修改成功";
                    logger.LogInformation("工作中心的修改成功，修改人：" + request.CreateName);
                }
                else
                {
                    result.Code = (int)CommonErrorCodes.Update;
                    result.Data = null;
                    result.Msg = "工作中心的修改失败";
                    logger.LogError("工作中心的修改失败，修改人：" + request.CreateName);

                }
            }
            catch (Exception ex)
            {
                logger.LogError($"工作中心的修改失败,修改人: {request.CreateName},错误信息：" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
